import java.sql.ResultSet;
import java.sql.SQLException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;


public class OpenBirthdaysDialog {

	public OpenBirthdaysDialog(Display display, Shell shell, OverviewTab ot) {
		final Shell dialog = new Shell(display, SWT.CLOSE);
		dialog.setLayout(new RowLayout(SWT.VERTICAL));
		final Table birthdaytable = new Table(dialog, SWT.FULL_SELECTION|SWT.BORDER);
		birthdaytable.setHeaderVisible(true);
		TableColumn[] birthdaycolumns = new TableColumn[5];
		for(int i = 0; i<5;i++){
			birthdaycolumns[i] = new TableColumn(birthdaytable, SWT.NONE);
			birthdaycolumns[i].setWidth(50);
		}
		birthdaycolumns[0].setText("Mitgliedsnr.");
		birthdaycolumns[1].setText("Nachname");
		birthdaycolumns[2].setText("Vorname");
		birthdaycolumns[3].setText("Geburtstag");
		birthdaycolumns[4].setText("Drucken");
		
	    ResultSet birthdayset = MySQLConnection.getBirthdays();
	    
		try {
			birthdayset.first();
			do
			{			    
				final TableItem item = new TableItem(birthdaytable, SWT.NONE);
				item.setText(0, birthdayset.getString("id"));
				item.setText(1, birthdayset.getString("lastname"));
				item.setText(2, birthdayset.getString("firstname"));
				item.setText(3, birthdayset.getString("birthday"));
				item.setText(4, "0");
				
			    TableEditor editor = new TableEditor(birthdaytable);
			    editor.horizontalAlignment = SWT.CENTER;
			    editor.grabHorizontal = true;
			    editor.minimumWidth = birthdaycolumns[4].getWidth();	
			    System.out.println(birthdaycolumns[4].getWidth());
	        	final Button check_or = new Button(birthdaytable,SWT.CHECK);
	        	check_or.addSelectionListener(new SelectionAdapter() {
			    	public void widgetSelected(SelectionEvent e) {
			    		if(item.getText(4).equals("0"))
			    			item.setText(4,"1");
			    		else
			    			item.setText(4,"0");
			    	}
	        	});
	        	if(birthdayset.getString("notified").equals("1"))
	        		check_or.setSelection(true);
	        	editor.setEditor(check_or, item, 4);	        	
			}while(birthdayset.next());
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		int width = 0;
		for(int i = 0; i<5;i++){
			birthdaycolumns[i].pack();
			width += birthdaycolumns[i].getWidth();			
		}
		
	    Composite buttons = new Composite(dialog, SWT.RIGHT_TO_LEFT);
	    buttons.setLayout(new RowLayout());
	    buttons.setLayoutData(new RowData(width,30));
	    Button save = new Button(buttons, SWT.NONE);
	    save.setText("Drucken");
	    save.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent event){
				BookRedemptions(birthdaytable);
				dialog.close();
			}
		});	
	    Button cancel = new Button(buttons, SWT.NONE);
	    cancel.setText("Abbrechen");
	    cancel.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent event){
				dialog.close();
			}
		});	
	    dialog.pack();
	    
	    Rectangle shellBounds = shell.getBounds();
	    Point dialogSize = dialog.getSize();
	    dialog.setLocation(
	      shellBounds.x + (shellBounds.width - dialogSize.x) / 2,
	      shellBounds.y + (shellBounds.height - dialogSize.y) / 2);
		dialog.open();
	}

	protected void BookRedemptions(Table birthdaytable) {
		for(int i=0;i<birthdaytable.getItemCount();i++){
			if(birthdaytable.getItem(i).getText(4).equals("1"))
				MySQLConnection.setnotified(birthdaytable.getItem(i).getText(0));
		}
	}
}
